package com.networkviwer;

import com.networkviwer.networkviewer.NetworkViewer;
import com.networkviwer.tasks.AutoDataTask;
import com.networkviwer.utils.H2DataBaseUtil;
import com.networkviwer.utils.UnionDatasourceUtil;

import javax.swing.*;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class Main {

    private static H2DataBaseUtil datasource = new H2DataBaseUtil();

    public static void main(String[] args) throws SQLException, ClassNotFoundException, IllegalAccessException, UnsupportedLookAndFeelException, InstantiationException, IOException {
        //设置UI风格
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        //启动H2数据库（嵌入式）
        doStartUpWork();
        //开启定时任务
        new Thread(new AutoDataTask()).start();
        //开启主程序
        new Thread(new NetworkViewer()).start();
    }

    private static void doStartUpWork() throws SQLException, ClassNotFoundException {
        datasource.contextInitialized();
        //创建所需表
        UnionDatasourceUtil.createTable();
        doShutDownWork();
    }

    private static void doShutDownWork() {
        Runtime run = Runtime.getRuntime();
        //注册新的虚拟机来关闭钩子
        run.addShutdownHook(new Thread(() -> {
            //程序结束时关闭H2数据库
            datasource.contextDestroyed();
        }));
    }

}
